package oot.data;

import oot.activity.FlashScreenActivity;

import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public class DoWithServer {
	private  Context mContext;
	private  JSONObject js;
	public DoWithServer(Context ct){
		mContext = ct;
		
	}
	public void doFavorite(Tweet t,boolean favorite){
		//d
		String api="";
		if(favorite)
			api= "/favorites/favorite";
		else
			api= "/favorites/unfavorite";
		RequestParams params = new RequestParams();
		if(t.getRetweet()==null)
			params.put("tweetid", t.getTweetId());
		else{
			params.put("tweetid", t.getRetweet().getTweetId());
		}
		params.put("userid", FlashScreenActivity.user.getUserID());
		Log.i("test", params.toString());
		// Connection.setBaseURL("http://192.168.1.9");
		Connection.post(api, params,
				new AsyncHttpResponseHandler() {
					public void onSuccess(String arg0) {
						Log.i("test", "return --------" + arg0);
						super.onSuccess(arg0);
						try {
							js = new JSONObject(arg0);
							if (js.getInt("status_code") != 0) {
								// show error message if error occur

								Toast toast = Toast.makeText(mContext,
										js.getString("message"),
										Toast.LENGTH_SHORT);
								toast.show();
							} else {
								Toast toast = Toast.makeText(mContext,
										js.getString("message"),
										Toast.LENGTH_SHORT);
								toast.show();
								//finish();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}
	public void doRetweet(Tweet t, boolean retweet){
		String api= "";
		if(retweet)
			api= "/tweets/retweet";
		else
			api= "/tweets/unretweet";
		RequestParams params = new RequestParams();
		if(t.getType().equals(Tweet.TYPE_RETWEET)){
			params.put("tweet_id", t.getRetweet().getTweetId());
		}
		else{
			params.put("tweet_id", t.getTweetId());
		}
		params.put("user_id", FlashScreenActivity.user.getUserID());
		Log.i("test", params.toString());
		// Connection.setBaseURL("http://192.168.1.9");
		Connection.post(api, params,
				new AsyncHttpResponseHandler() {
					public void onSuccess(String arg0) {
						Log.i("test", "return --------" + arg0);
						super.onSuccess(arg0);
						try {
							js = new JSONObject(arg0);
							if (js.getInt("status_code") != 0) {
								// show error message if error occur

								Toast toast = Toast.makeText(mContext,
										js.getString("message"),
										Toast.LENGTH_SHORT);
								toast.show();
							} else {
								Toast toast = Toast.makeText(mContext,
										js.getString("message"),
										Toast.LENGTH_SHORT);
								toast.show();
								//finish();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}
	public void doDelete(Tweet t){
		String api= "/Tweets/delete";
		RequestParams params = new RequestParams();
		params.put("tweet_id", t.getTweetId());
		params.put("user_id", FlashScreenActivity.user.getUserID());
		Log.i("test", params.toString());
		// Connection.setBaseURL("http://192.168.1.9");
		Connection.post(api, params,
				new AsyncHttpResponseHandler() {
					public void onSuccess(String arg0) {
						Log.i("test", "return --------" + arg0);
						super.onSuccess(arg0);
						try {
							js = new JSONObject(arg0);
							if (js.getInt("status_code") != 0) {
								// show error message if error occur

								Toast toast = Toast.makeText(mContext,
										js.getString("message"),
										Toast.LENGTH_SHORT);
								toast.show();
							} else {
								Toast toast = Toast.makeText(mContext,
										js.getString("message"),
										Toast.LENGTH_SHORT);
								toast.show();
								//finish();
							}
						} catch (JSONException e) {
							// TODO Auto-generated catch block
							e.printStackTrace();
						}
					}
				});
	}
}
